﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class LanguagePackTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }
        static readonly Dictionary<String, String> TestPackDic = new Dictionary<String, String>
        {
            {"Section1.Section11.Str1", "1_11_1"},
            {"Section1.Section11.Str2", "1_11_2"},
            {"Section1.Section11.Str3", "1_11_3"},
            {"Section1.Section12.Str1", "1_12_1"},
            {"Section1.Section12.Str2", "1_12_2"},
            {"Section2.Section21.Str1", "2_21_1"},
            {"Section2.Section21.Str2", "2_21_2"},
            {"Section2.Section22.Str1", "2_22_1"},
            {"Section2.Section23.Str1", "2_23_1"},
            {"Section2.Section23.Str2", "2_23_2"},
            {"Section2.Section23.Str3", "2_23_3"},
            {"Section3.Section31.Section311.Str1", "3_31_311_1"},
            {"Section3.Section31.Section312.Str1", "3_31_312_1"},
            {"Section3.Section32.Section321.Str1", "3_32_321_1"},
            {"Section3.Section32.Section322.Str1", "3_32_322_1"}
        };

        [TestCase("xy")]
        public void Can_CRUD(string code)
        {
            var language = Processor.Execute(new CreateLanguage
            {
                Code = "xy"
            }).Result;
            var pack = Processor.Execute(new CreateLanguagePack
            {
                Code = code,
                LanguageID = language.EntityID
            }).Result;

            Assert.NotNull(pack);
            pack.Code.AreEqualIgnoringCase(code);
            pack.AreEqual(Processor.Execute(new GetLanguagePack
            {
                EntityID = pack.EntityID
            }).Result);
            pack.AreEqual(Processor.Execute(new GetLanguagePackByCode
            {
                LanguageID = language.EntityID,
                Code = code
            }).Result);

            code = code.Reverse();
            var upd_pack = Processor.Execute(new UpdateLanguagePack
            {
                EntityID = pack.EntityID,
                LanguageID = pack.Language.EntityID,
                Code = code
            }).Result;

            Assert.NotNull(upd_pack);
            upd_pack.AreEqual(pack);
            upd_pack.Code.AreEqualIgnoringCase(code);

            Processor.Execute(new DeleteLanguagePack
            {
                EntityID = pack.EntityID
            });
        }

        [Test]
        public void Cannot_CreateUpdate_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateLanguagePack
            {
                Code = String.Empty
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateLanguagePack
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Can_GetSetContent()
        {
            var pack = Processor.Execute(new CreateLanguagePack
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID
            }).Result;

            Processor.Execute(new SetLanguagePackContent
            {
                EntityID = pack.EntityID,
                Content = TestPackDic
            });

            var dic = Processor.Execute(new GetLanguagePackContent
            {
                EntityID = pack.EntityID
            }).Result;

            CollectionAssert.AreEqual(TestPackDic, dic);
        }
    }
}
